#ifndef UDFMODEL_H
#define UDFMODEL_H

#include "protocol.h"

#include <QFile>
#include <QVector>
#include <QDataStream>

#include "dataview.h"

struct UDF {
    quint32 SID;
    quint32 DID;
    quint16 MID;
    quint32 BID;
    quint32 Res;
    quint32 JS;
    quint16 LEN;

    QTime time(void);
};

QDataStream& operator >>(QDataStream& is, UDF& udf);

class UDFModel : public ProtocolTableModel
{
    Q_OBJECT
public:
    UDFModel(const QString& fileName, QObject *parent = 0);

signals:
    void openTempFile(const QString fileName);

public slots:

private:
    mutable QFile file;
    QVector<qint64> records;
    qint64 showCount;
    mutable int cacheRow;
    mutable UDF udf;
    mutable QByteArray record;

    void loadRecord(int row) const;
    bool calcRecords(void);
    int maxDisplaySize(void) const;
    void filterSize(const QModelIndex& index);
    void filterMID(const QModelIndex& index);
    void filterSID(const QModelIndex& index);
    void filterDID(const QModelIndex& index);
    void filterBID(const QModelIndex& index);
    void filterJS(const QModelIndex& index);
    void filterLEN(const QModelIndex& index);

    // QAbstractItemModel interface
public:
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    void fetchMore(const QModelIndex &parent);
    bool canFetchMore(const QModelIndex &parent) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

    // ProtocolTableModel interface
public:
    qint64 position(const QModelIndex &index);
    QModelIndex locate(qint64 pos);
    bool deleteRows(const QString &fileName, const QModelIndexList &rows);
    bool deleteColumns(const QString &fileName, const QModelIndexList &cols);
    bool saveRows(const QString &fileName, const QModelIndexList &rows);
    bool saveColumns(const QString &fileName, const QModelIndexList &cols);
    bool supportFilter(const QModelIndex& index);
    void filter(const QModelIndex& index);
};


#endif // UDFMODEL_H
